<?php

namespace app\middleware;

use app\model\StationBasic;
use Closure;
use think\facade\Cache;
use think\Request;

class GasMiddleware
{
    public function handle(Request $request, Closure $next)
    {
        if ($request->isOptions()) {
            return response('', 204);
        }
        $author = $request->header('Authorization', '[]');
        $author = json_decode($author, true);
        if (empty($author)) throw new \Exception("请先登录", 70354);
        if (!Cache::has("gas_" . $author['token'])) throw new \Exception("请先登录", 70354);
        $admin = Cache::get("gas_" . $author['token']);
        $user = app(StationBasic::class)->where("id", $admin['id'])->where('state', 1)->find();
        if (empty($user)) throw new \Exception("请先登录", 70354);
        $request->station = $user;
        return $next($request);
    }
}
